<?php
/**
 * The SIDN EPP Result object
 *
 * <Insert random introduction here...>
 *
 *
 *
 * @author Ralph de Boom <php5-sidnepp@mijndomein.nl>
 * @author Peter Keizer <php5-sidnepp@mijndomein.nl>
 * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License v3
 * @copyright http://www.mijndomein.nl/
 * @version 0.5
 * @link http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/index.html
 * @see http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/php5-sidnepp/SidnContactPostalInfo.html
 * @package php5-sidnepp
 */

class SidnContactPostalInfo
{
	private $name;
	private $organisationName;
	private $street;
	private $city;
	private $province;
	private $zipcode;
	private $countrycode;
	/**
	 *
	 * @param string $name
	 * @param string $city
	 * @param string $countrycode
	 * @param string $organisationName
	 * @param string $street
	 * @param string $province
	 * @param string $zipcode
	 */
	public function __construct($name, $city, $countrycode, $organisationName = null, $street = null, $province = null, $zipcode = null)
	{
		$this->setName($name);
		if ($street)
		{
			$this->addStreet($street);
		}
		$this->setOrganisationName($organisationName);
		$this->setCity($city);
		$this->setProvince($province);
		$this->setZipcode($zipcode);
		$this->setCountrycode($countrycode);
	}
	/**
	 * Add a street line
	 * @param string $street
	 * @return void
	 */
	public function addStreet($street)
	{
		if (count($this->street) < 3)
		{
			$this->street[count($this->street)] = $street;
		}
		else
		{
			throw new SidnEppException('You can\'t add more than 3 streets for one address!');
		}

	}
	/**
	 * Gets a street by given line number
	 * @param int $line
	 * @return string
	 */
	public function getStreet($line)
	{
		if ($this->street[$line])
		{
			return $this->street[$line];
		}
		else
		{
			return null;
		}

	}
	/**
	 * Sets the organisation name
	 * @param string $organisationName
	 * @return void
	 */
	public function setOrganisationName($organisationName)
	{
		$this->organisationName = $organisationName;
	}
	/**
	 * Gets the organisation name
	 * @return string
	 */
	public function getOrganisationName()
	{
		return $this->organisationName;
	}
	/**
	 * Sets the name of the person
	 * @param string $name
	 * @return void
	 */
	public function setName($name)
	{
		$this->name = $name;
	}
	/**
	 * Gets the name
	 * @return string
	 */
	public function getName()
	{
		return $this->name;
	}
	/**
	 * Sets the city of residence
	 * @param string $city
	 * @return void
	 */
	public function setCity($city)
	{
		$this->city = $city;

	}
	/**
	 * Gets the city
	 * @return string
	 */
	public function getCity()
	{
		return $this->city;

	}
	/**
	 * Sets the zipcode
	 * @param string $zipcode
	 * @return void
	 */
	public function setZipcode($zipcode)
	{
		//Remove garbage from the zipcode
		$zipcode = preg_replace('/[^a-z\d]/i', '', $zipcode);
		$this->zipcode = $zipcode;

	}
	/**
	 * Gets the zipcode
	 * @return string
	 */
	public function getZipcode()
	{
		return $this->zipcode;

	}
	/**
	 * Sets the province
	 * @param string $province
	 * @return void
	 */
	public function setProvince($province)
	{
		$this->province = $province;

	}
	/**
	 * Gets the province
	 * @return string
	 */
	public function getProvince()
	{
		return $this->province;

	}
	/**
	 * Sets the countrycode
	 * @param string $countrycode
	 * @link http://xml.coverpages.org/country3166.html
	 * @return void
	 */
	public function setCountrycode($countrycode)
	{
		$this->countrycode = $countrycode;
	}
	/**
	 * Gets the countrycode
	 * @link http://xml.coverpages.org/country3166.html
	 * @return string
	 */
	public function getCountrycode()
	{
		return $this->countrycode;

	}
	/**
	 * Generates XML notation of the current object
	 * @return string
	 */

	/**
	 * Generates XML notation of the current object
	 * @param boolean $update: Set to true if you are using xml to update contact info.
	 *	                       This will omit the name field, because a name may not be altered with update command
	 * @return string
	 */
	public function toXml($update = false)
	{
		if (!$update)
		{
			$vars['name']				= '<contact:name><![CDATA[' . $this->getName() . ']]></contact:name>';
		}

		if (strlen($this->getOrganisationName()))
		{
			$vars['organisationName']	= '<contact:org><![CDATA['.$this->getOrganisationName().']]></contact:org>';
		}
		if (count($this->street))
		{
			$vars['street'] = '';
			foreach ($this->street as $street)
			{
				$vars['street'] .= '<contact:street><![CDATA['.$street.']]></contact:street>';
			}
		}
		$vars['city']				= $this->getCity();
		if (strlen($this->getProvince()))
		{
			$vars['province']			= '<contact:sp><![CDATA['.$this->getProvince().']]></contact:sp>';
		}
		if (strlen($this->getZipcode()))
		{
			$vars['zipcode']			= '<contact:pc><![CDATA['.$this->getZipcode().']]></contact:pc>';
		}
		$vars['countrycode']		= $this->getCountrycode();
		$postalInfoXml = SidnTools::getXml('contact_postalinfo.xml', $vars);
		return $postalInfoXml;
	}
}